<template>
  <div>
    <el-dialog
      :title="eject.isadd == true ? '用户添加' : '用户修改'"
      :visible.sync="eject.isshow"
      @closed="guanbi"
    >
      <el-form :model="form">


        <el-form-item label="所属角色" label-width="120px">
          <!-- <el-select v-model="form.name">
            <el-option value disabled>请选择</el-option>
            
            
            <el-option value="item.name" v-for="item in form" :key='item.pid'>
               
            </el-option>

          </el-select> -->
          {{ form }}
          <el-select v-model="form.roleid">
            
            <el-option
              v-for="item in name"
              :key="item.id"
              :label="item.rolename"
              :value="item.id"
            >
            </el-option>

            <!-- <el-option>主任</el-option> -->
          </el-select>
        </el-form-item>


        <el-form-item label="用户名" label-width="120px" :rules="rules">
          <el-input v-model="form.username" autocomplete="off" required></el-input>
        </el-form-item>

        <el-form-item label="密码" label-width="120px" :rules="rules">
          <el-input v-model="form.password" autocomplete="off" required></el-input>
          <div>留空则不修改</div>
        </el-form-item>


        <el-form-item label="状态" label-width="120px">
          <el-switch
            v-model="form.status"
            :active-value="1"
            :inaactive-value='2'
          ></el-switch>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="add()" v-if="eject.isadd"
          >添 加</el-button
        >
        <el-button type="primary" @click="update" v-else>编 辑</el-button>
        <el-button @click="close">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { IndexRouter } from "../../../router/index.js";
import {
  reqmenuadd,
  reqmenuone,
  reqmenuedit,
  reqrolelist,
  requseradd,
  requserone,
  requseredit
} from "../../../request/api";
import { successAlert } from "../../../utils/alert";
export default {
  props: ["eject", "list"],
  data() {
    return {
      form: {
        roleid: '',
        username: "",
        password: "",
        status:1
      },
      name: [],
      icons: [
        "el-icon-s-tools",
        "el-icon-user-solid",
        "el-icon-s-goods",
        "el-icon-help",
        "el-icon-s-operation",
      ],
      IndexRouter,
      rules: [
        {
          required: true,
        },
      ],
    };
  },
  created() {},
  mounted() {
    reqrolelist().then((res) => {
      // console.log(res);
      this.name = res.data.list;
      // console.log(this.form);
    });
  },
  methods: {
    guanbi() {
      !this.eject.isadd && this.empty();
    },
    close() {
      this.$emit("close");
    },
    // 封装一个清空方法
    empty() {
      this.form = {
         roleid: "",
        username: "",
        password: "",
        status:1
      };
    },
    //点击添加按钮
    add() {
    
      // this.$emit("add");
      // 发起请求
      requseradd(this.form).then((res) => {
        console.log(res);
        // 添加成功
        if (res.data.code == 200) {
          // 弹出成功
          successAlert(res.data.msg);
          //清空输入框
          this.empty();
          // 弹框消失
          this.eject.isshow = false;
          // 重新列表请求
          this.$emit("init");
        }
      });

   
    },
    update() {
      // 请求接口
      requseredit(this.form).then((res) => {
        console.log(res);
        if (res.data.code == 200) {
          // 弹出成功提示框
          successAlert(res.data.msg);
          // 清空输入框
          this.empty();
          // 关闭弹窗
          this.eject.isshow = false;
          // 通知父组件更新列表
          this.$emit("update");
        }
      });
    },
    changepid() {
      this.form.type = this.form.pid == 0 ? 1 : 2;
    },
    // 获取一条信息方法，父组件通过ref调用
    getone(id) {
      
 
      requserone({ uid: id }).then((res) => {
       
        this.form = res.data.list;
        //********************************* */
        this.form.password=""
        console.log(this.form);
      });
    },
  },
};
</script>

<style scoped >
</style>
